//
//  LoginViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/15.
//  Copyright © 2016年 苟亚明. All rights reserved.
// 15281696851 a2122250

#import "LoginViewController.h"
#import "CALayer+Extension.h"
#import "rootTabBarViewController.h"
#import "KPNaviViewController.h"
#import "registViewController.h"
#import "forgetPasswordViewController.h"
#import "SVProgressHUD.h"
#import "registerViewController.h"
#import "forgetPasswordStep1ViewController.h"
#import <AFNetworking.h>
#import "loginTextField.h"
#import "MBProgressHUD+MJ.h"
#import "userModel.h"
#import <MJExtension.h>
#include "userDefault.h"
@interface LoginViewController ()
@property (nonatomic, strong) rootTabBarViewController *rootTabBarViewController;
@property (weak, nonatomic) IBOutlet loginTextField *textUsername;
/** 管理者 */
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@property (weak, nonatomic) IBOutlet loginTextField *textPassword;
@property (nonatomic, strong) NSArray *arrUser;
@end

@implementation LoginViewController
- (IBAction)btnFogetPasswodClick:(id)sender {
    forgetPasswordStep1ViewController  *vc = [[forgetPasswordStep1ViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    
    

}
- (IBAction)btnLoginClick:(id)sender {
    
    if(self.textUsername.text.length != 11 && self.textPassword.text.length < 6){
        [MBProgressHUD showError:@"输入错误"];
        return;
    }
   
    
    // 显示指示器
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    // 登录请求
    NSString *url = [NSString stringWithFormat:@"%@%@",GYMRequeatHost,GYMLogin];
    
    [self.manager POST:url parameters:@{@"username":self.textUsername.text,@"password":self.textPassword.text} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    
        // 隐藏指示器
        [SVProgressHUD dismiss];
        if (![responseObject[@"msg"] isEqualToString:@"登录成功"]) {
            [MBProgressHUD showError:@"登录失败"];
            return ;
        }
        NSLog(@"responseObject = %@",responseObject);
        
        userModel *model = [userModel sharedManager];
        
        model = [userModel mj_objectWithKeyValues:responseObject[@"result"]];
        
        //保存帐号密码
        [userDefault setValue:_textUsername.text forKey:@"userName"];
        [userDefault setValue:_textPassword.text forKey:@"passWord"];
        
    
        rootTabBarViewController *vc = [rootTabBarViewController sharedRootVC];
        self.rootTabBarViewController = vc;
        self.rootTabBarViewController.navigationController.navigationBar.hidden = NO;
        
        self.view.window.rootViewController = self.rootTabBarViewController;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 隐藏指示器
        [SVProgressHUD dismiss];
        NSLog(@"error = %@",error);
    }];

}
- (IBAction)btnRegistClick:(id)sender {
    
    registerViewController *vc = [[registerViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)dealloc
{
    GYMLog(@"LoginViewController --- dealloc");
}


- (NSArray *)arrUser{
    if (!_arrUser) {
        _arrUser = [NSArray array];
    }
    return _arrUser;
}

- (AFHTTPSessionManager *)manager{
    if (!_manager) {
        _manager = [AFHTTPSessionManager manager];
        //申明返回的结果是json类型
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        _manager.requestSerializer=[AFJSONRequestSerializer serializer];
    }
    return _manager;
}

@end
